package blog.controller;

import blog.entity.BatchResult;
import blog.entity.QueryJson;
import blog.entity.ResultJson;
import blog.pojo.ChapterCategory;
import blog.service.ChapterCategoryService;
import blog.service.ChapterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/chapterCategory")
public class ChapterCategoryController {

    @Autowired
    private ChapterCategoryService chapterCategoryService;

    @Autowired
    private ChapterService chapterService;


    @RequestMapping("/findPage")
    public ResultJson findPage(@RequestBody QueryJson<ChapterCategory> queryJson) throws Exception{
        if(queryJson.getPage() == null) return ResultJson.ARGUMENT_NULL;

        IPage<ChapterCategory> page = chapterCategoryService.getPage(queryJson.getPage(), queryJson.getEntity(), queryJson.getKeywords());
        return ResultJson.isSuccess(page);
    }

    @RequestMapping("/edit")
    public ResultJson edit(@RequestBody QueryJson<ChapterCategory> queryJson) throws Exception{
        if(queryJson.getEntity() == null) return ResultJson.ARGUMENT_NULL;

        if(queryJson.getEntity().getId() == null){
            if(!chapterCategoryService.insert(queryJson.getEntity())){
                return ResultJson.INSERT_ERROR;
            }
        } else {
            if(queryJson.getEntity().getIsuse() == 2 || queryJson.getEntity().getIsuse() == 0) {
                if(chapterService.selectCountByCategoryId(queryJson.getEntity().getId()) > 0 ){
                    return ResultJson.UPDATE_USED_ERROR;
                }
                if(chapterCategoryService.selectCountByPid(queryJson.getEntity().getId()) > 0){
                    return ResultJson.UPDATE_USED_ERROR;
                }
            }
            if(!chapterCategoryService.update(queryJson.getEntity())){
                return ResultJson.UPDATE_ERROR;
            }
        }
        return ResultJson.SUCCESS;
    }

    @RequestMapping("/updateManyByIds")
    public ResultJson updateManyByIds(@RequestBody QueryJson<ChapterCategory> queryJson) throws Exception{
        if(queryJson.getEntity() == null || queryJson.getIds() == null){
            return ResultJson.ARGUMENT_NULL;
        }
        int success = 0, fail = 0, usedFail = 0;
        for(Integer id : queryJson.getIds()){
            ChapterCategory chapterCategory = queryJson.getEntity().setId(id);
            if(chapterCategory.getIsuse() == 2 || chapterCategory.getIsuse() == 0) {
                if(chapterService.selectCountByCategoryId(chapterCategory.getId()) > 0 ){
                    usedFail++;
                    fail++;
                    continue;
                }
            }
            if(chapterCategoryService.selectCountByPid(id) > 0){
                usedFail++;
                fail++;
                continue;
            }
            if (chapterCategoryService.update(chapterCategory)) {
                success++;
            } else {
                fail++;
            }
        }
        if(usedFail > 0){
           return ResultJson.UPDATE_PART_USED_ERROR;
        }
        return ResultJson.isSuccess(BatchResult.SET(success,fail));
    }

    @RequestMapping("/getCategoryList")
    public ResultJson getCategoryList(@RequestBody QueryJson<ChapterCategory> queryJson) throws Exception{
        List<ChapterCategory> all = chapterCategoryService.getCategoryList(Integer.valueOf(queryJson.getOthers().get("level")));
        return ResultJson.isSuccess(all);
    }

    @RequestMapping("/find")
    public ResultJson find(@RequestBody QueryJson<ChapterCategory> queryJson) throws Exception{
        if(queryJson.getEntity() == null) {
            return ResultJson.ARGUMENT_NULL;
        }
        return ResultJson.isSuccess(chapterCategoryService.get(queryJson.getEntity()));
    }
    @RequestMapping("findAll")
    public ResultJson findAll() throws Exception{
        return ResultJson.isSuccess(chapterCategoryService.getAll());
    }
}
